<?php
/**
 * 
 * Enter description : Modele thao tác gửi mail
 * @author admin888
 *
 */
class Model_Guithu extends Zend_Db_Table
{
	protected  $_chus = 'cau_hinh_user';
	protected $_gt = 'thu_gui';
	protected $_file = 'dinh_kem';
	protected $_tk = 'tu_khoa_email';
	
	
	public function __construct()
	{
	  $this->_db =  Zend_Registry::get('connectDb');
	  $this->_date =   date('d-m-Y H:i:s');
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description here ...
	 * @param unknown_type $uid
	 */
	public function  getthongtinguithu($uid)
	{
		$sql = $this->_db->select()
						 ->from($this->_chus)
						 ->where('uid =? ',$uid);
			$query = $this->_db->fetchRow($sql);
		return $query;			 
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : them thu nháp 
	 * @param unknown_type $uid
	 * @param unknown_type $data
	 */
	public function addthunhap($uid,$data)
	{
			 
			
			$arr = array(
				'uid'				=> $uid,
				'id_lh'				=>$data['arr_idlh'],
				'email_nguoinhan'	=>$data['arr_email'],
				'show_nguoinhan'	=>stripslashes($data['show_nguoinhan']),
				'cc_email'			=>$data['cc_'],
				'bcc_email'			=>$data['bcc_'],	
				'tieu_de'			=>$data['chu_de'],	
				'tk_id'  			=>$data['idtukhoa'],	
				'noi_nung'			=>stripslashes($data['noidung_thu_soan']),	
				'tong_files'		=>$data['tong_file'],	
				'ngay_tao'		    =>$this->_date,	
				'ngay_sua'		    =>$this->_date	
				
			);
			$query = $this->_db->insert($this->_gt, $arr);
			$idvuais = $this->_db->lastInsertId($this->_gt,'id_tm');
	  return  $idvuais;
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : thực hiện lưu thư đã gửi 
	 * @param unknown_type $uid
	 * @param unknown_type $data
	 */
	public function luuthudagui($uid,$data,$thongso)
	{
			$shownguoinhan = stripslashes($data['show_nguoinhan']);
			if ($shownguoinhan  == 0 ){
				$shownh = '';
			}else{
				$shownh = $shownguoinhan;
			} 
			$arr = array(
				'uid'				=> $uid,
				'id_lh'				=>$data['arr_idlh'],
				'email_nguoinhan'	=>$data['arr_email'],
				'show_nguoinhan'	=>$shownh,
				'cc_email'			=>$data['cc_'],
				'bcc_email'			=>$data['bcc_'],	
				'tieu_de'			=>$data['chu_de'],
			    'tk_id'  			=>$data['idtukhoa'],	
				'noi_nung'			=>stripslashes($data['noidung_thu_soan']),	
				'tong_files'		=>$data['tong_file'],
				'da_gui'			=>$thongso,	
				'ngay_tao'		    =>$this->_date,	
				'ngay_sua'		    =>$this->_date	
				
			);
		$query = $this->_db->insert($this->_gt, $arr);
		$lasid = $this->_db->lastInsertId($this->_gt,'id_tm');
	return $lasid;			
		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * @param unknown_type $uid	 
	 * Enter description : logon smpt mail server theo thông tin cấu hình
	 */
	public function loginsmtp($uid){
		$ts = new Model_Caidat();
		$thongso = $ts->getthongsotheoid($uid);
		$callemal = new Zend_Mail_Transport_Smtp( trim($thongso['SMTP_server']),
										array (
												'auth' => 'login',
												'username' => trim ($thongso['user_name_email']),
												'password' => trim( base64_decode($thongso['pass_email'])),
												'ssl' => trim($thongso['loai_mahoa']),
												'port' => trim($thongso['SMTP_port'])
												)
				 );
		return $callemal;		 		
	}
	/**
	 * admin888 <doan.hut@gmail.com>
	 * Enter description : thuc thi luu file vao database
	 * @param unknown_type $iddn
	 * @param unknown_type $idthuvualu
	 * @param unknown_type $data
	 */
	public function themfilevadb($iddn,$idthuvualu,$loaithu,$data)
	{
		$tenfiel = $data['file'];
		$tongfile = count($tenfiel);
		for ($i= 0 ; $i< $tongfile;$i++){
			//inser vao bang file
			$arr = array(
					'link_file'		=>$tenfiel[$i],
					'loai_thu'		=>$loaithu,
					'id_t'			=>$idthuvualu,
					'uid'           =>$iddn
			);
			$query = $this->_db->insert($this->_file, $arr);
		}
		return $query;
		//echo "<pre>"; print_r($tenfiel);die();
	
	}

	/**
	 * admin888 <doan.hut@gmail.com>
	 * Description : kiểm tra trong conten có key work hay không  
	 * @param unknown_type $conten
	 * @param unknown_type $tk
	 */
	function  checkkeywork($conten,$tk){
		$sql = $this->_db->select()
						 ->from($this->_tk,'ten_tu_khoa')
						 ->where('tk_id = ?',$tk);
		$query = $this->_db->fetchOne($sql);
		$iskey = strpos($conten, $query);
		if ($iskey >0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * admin888 <doan.hut@gmail.com>
	 * Description : trộn key và từ khoa 
	 * @param unknown_type $tk
	 * @param unknown_type $lhid
	 * @param unknown_type $conten
	 */
	public function trontukhoavoilienhe($uid,$tukhoa,$emaillh,$conten)
	{
		
		$tk = new Model_Tukhoa();
		$mangtukhoa = explode(',', $tukhoa);
		for ($i =0; $i< count($mangtukhoa);$i++){
			$tukhoatrave = $tk->gettukhoa($uid,$mangtukhoa[$i],$emaillh);
			//echo "<pre>"; print_r($tukhoatrave);
			foreach ($tukhoatrave as $key => $value){
		    $tucanthay= "/".$value['ten_tu_khoa']."/";
			$contenkey = preg_replace($tucanthay,$value['lienhe'],$conten);
			
			}
			
			return  $contenkey;
			//echo  "<pre>"; print_r($tukhoatrave);
			
		}
		//echo $contenkey;
		//die();
		
	}
	/**
	 * admin888 mịa nó làm lại lần mày quết làm ra thì thui 
	 * Enter description here ...
	 * @param unknown_type $lhid
	 * @param unknown_type $conten
	 */
	public function getcontentron($lhid,$conten)
	{
		$tk = new Model_Tukhoa();
		$alltukhoa = $tk->getalltukhoalienhe($lhid);
		//echo "<pre>"; print_r($alltukhoa);
		foreach ($alltukhoa as  $value){
				$tukhoa = "/".$value['ten_tu_khoa']."/";
				 $contenkey = preg_replace($tukhoa,$value[$tukhoa],$conten);
				 echo $contenkey;
		}
		die();
		//return $contenkey;
	}
}